package reflect;

import java.lang.reflect.Constructor;

public class ReflectDemo3 {
    public static void main(String[] args) throws Exception {
        Class cls = Class.forName("reflect.Person");
        //获取无参构造器
        Constructor constructor = cls.getConstructor();
        Object obj = constructor.newInstance();
        System.out.println(obj);//Person{name='null', age=0}
        //获取String构造器
        Constructor constructor1 = cls.getConstructor(String.class);
        Object obj1 = constructor1.newInstance("张三");
        System.out.println(obj1);//Person{name='张三', age=0}
        //获取String+int构造器
        Constructor constructor2 = cls.getConstructor(String.class,int.class);
        Object obj2 = constructor2.newInstance("李四",30);
        System.out.println(obj2);//Person{name='李四', age=30}
    }
}
